package com.store.app.imlife.activity;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;

import com.hyphenate.EMCallBack;
import com.hyphenate.chat.EMClient;
import com.store.app.R;
import com.store.app.hyphenate.Constant;
import com.store.app.hyphenate.HyphenateHelper;

public class Imlife_MainActivity extends Activity implements  View.OnClickListener {

    private Button bt_conversation_list;
    private Button bt_conversation;
    private Button bt_livechat;
    private Button bt_qupai;
    private Button btn_user1,btn_user2,btn_user3,btn_user4;
    public static String url="";
    public static String name="";
    private Button btn_logout;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.imlife_activity_main);
        initView();
    }




    private void initView() {
        bt_conversation_list = (Button) findViewById(R.id.bt_chatandvideo);
        bt_conversation_list.setOnClickListener(this);
        bt_conversation = (Button) findViewById(R.id.bt_conversation);
        bt_conversation.setOnClickListener(this);
        bt_livechat = (Button) findViewById(R.id.bt_livechat);
        bt_livechat.setOnClickListener(this);
        bt_qupai= (Button) findViewById(R.id.bt_qupai);
        bt_qupai.setOnClickListener(this);
        btn_user1=(Button) findViewById(R.id.user1);
        btn_user1.setOnClickListener(this);
        btn_user2=(Button) findViewById(R.id.user2);
        btn_user2.setOnClickListener(this);
        btn_user3=(Button) findViewById(R.id.user3);
        btn_user3.setOnClickListener(this);
        btn_user4=(Button) findViewById(R.id.user4);
        btn_user4.setOnClickListener(this);
        btn_logout=(Button) findViewById(R.id.logout);
        btn_logout.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.bt_chatandvideo:
                if(TextUtils.isEmpty(name)){
                    Toast.makeText(Imlife_MainActivity.this,"请先点击下面用户登录！",Toast.LENGTH_SHORT).show();
                }else {
                    Intent intent = new Intent(this, ChatAndVideoActivity.class);
                    intent.putExtra("chatType", Constant.CHATTYPE_GROUP);
                    intent.putExtra("userId", "1469697628965");
                    startActivity(intent);
                }
                break;
            case R.id.bt_conversation:
                if(TextUtils.isEmpty(name)){
                    Toast.makeText(Imlife_MainActivity.this,"请先点击下面用户登录！",Toast.LENGTH_SHORT).show();
                }else {
                    Intent intent = new Intent(this, ChatActivity.class);
                    intent.putExtra("chatType", Constant.CHATTYPE_GROUP);
                    intent.putExtra("userId", "1469697628965");
                    startActivity(intent);
                }
                break;
            case R.id.bt_livechat:

                break;
            case R.id.bt_qupai:
                Intent intent_qupai = new Intent(this, LiveCameraActivity.class);
                startActivity(intent_qupai);
                break;
            case R.id.user1:
                regist("nba11");
                break;
            case R.id.user2:
                regist("nba12");
                break;
            case R.id.user3:
                regist("nba13");
                break;
            case R.id.user4:
                regist("nba14");
                break;
            case R.id.logout:
                logout();
                break;
        }
    }
    public void regist(String name){
          login(name);
    }
    public void login(final String s_name){
        EMClient.getInstance().login(s_name, "123456", new EMCallBack() {

            @Override
            public void onSuccess() {
                runOnUiThread(new Runnable() {
                    public void run() {
                        Imlife_MainActivity.name=s_name;
                        Toast.makeText(getApplicationContext(), "登录成功",
                                Toast.LENGTH_SHORT).show();
                    }
                });


                // ** manually load all local groups and conversation
                EMClient.getInstance().groupManager().loadAllGroups();
                EMClient.getInstance().chatManager().loadAllConversations();

            }

            @Override
            public void onProgress(int progress, String status) {
                Log.d("mylog", "login: "+progress);
            }

            @Override
            public void onError(final int code, final String message) {
                Log.d("mylog", "login: err："+message);
                runOnUiThread(new Runnable() {
                    public void run() {
                        Toast.makeText(getApplicationContext(), "请先退出登录",
                                Toast.LENGTH_SHORT).show();
                    }
                });
            }
        });
    }
    public  void logout(){
    HyphenateHelper.getInstance().logout(true,new EMCallBack() {

        @Override
        public void onSuccess() {
            runOnUiThread(new Runnable() {
                public void run() {
            Toast.makeText(getApplicationContext(), "退出登录成功",
                    Toast.LENGTH_SHORT).show();
                }
            });
        }

        @Override
        public void onProgress(int progress, String status) {

        }

        @Override
        public void onError(int code, String message) {

        }
    });
    }
}
